float4x4 gWVP;
float4x4 gWorld;
float3 gEyePos;

float3 gLightDir0;
float3 gLightDir1;
float3 gLightDir2;
float4x3 gBones[58];

struct VertexData
{
  float4 Pos : POSITION;
  float3 Normal : NORMAL0;
  float2 TexCoord : TEXCOORD0;
  int4 Indices : BLENDINDICES0;
  float4 Weights : BLENDWEIGHT0;
};

struct VertexOut
{
  float4 Pos : POSITION0;
  float3 NormalWorld : TEXCOORD1;
  float2 TexCoord : TEXCOORD0;
  float3 HalfLight0 : TEXCOORD2;
  float3 HalfLight1 : TEXCOORD3;
  float3 HalfLight2 : TEXCOORD4;
};

VertexOut main(VertexData i)
{
   VertexOut o;
   
   float3 finalPos = 0;
   float3 finalNorm = 0;

   for(int iBone = 0; iBone<4; iBone++)
   {
		finalPos += mul(i.Pos, gBones[i.Indices[iBone]]) * i.Weights[iBone];
        finalNorm += mul(i.Normal, (float3x3)gBones[i.Indices[iBone]]) * i.Weights[iBone];

   }

   i.Pos.xyz = finalPos;
   i.Normal = finalNorm;
   o.Pos = mul(i.Pos, gWVP);   
   o.NormalWorld = mul(i.Normal, (float3x3)gWorld);
   float3 posInWorld = mul(i.Pos, gWorld).xyz;
   float3 posToEye = normalize(gEyePos - posInWorld);
   o.HalfLight0 = normalize(posToEye - gLightDir0);
   o.HalfLight1 = normalize(posToEye - gLightDir1);
   o.HalfLight2 = normalize(posToEye - gLightDir2);

   o.TexCoord = i.TexCoord;
   return o;
};